/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ws;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public abstract class BaseServer {

    private int port;
    ServerBootstrap bootstrap;
    ChannelFuture f;
    private int workcount=8;
    public BaseServer(int port) throws Exception {
        this.port = port;

    }
     public BaseServer(int port,int workcount) throws Exception {
        this.port = port;
        this.workcount=workcount;

    }

    public void run() throws Exception {

        EventLoopGroup bossGroup = new NioEventLoopGroup(workcount); // (1)
        EventLoopGroup workerGroup = new NioEventLoopGroup(workcount);
        try {
            ServerBootstrap b = new ServerBootstrap(); // (2)
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class) // (3)
                    .childHandler(new WebsocketChatServerInitializer(this)) //(4)
                    .option(ChannelOption.SO_BACKLOG, 128) // (5)
                    .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)

            System.out.println("WebsocketChatServer 启动了" + port);

            // 绑定端口，开始接收进来的连接
             f = b.bind(port).sync(); // (7)

            // 等待服务器  socket 关闭 。
            // 在这个例子中，这不会发生，但你可以优雅地关闭你的服务器。
            f.channel().closeFuture().sync();

        }catch(Exception e){
            e.printStackTrace();
        } finally {
            
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();

            System.out.println("WebsocketChatServer 关闭了");
        }
    }

    public abstract void onOpen(ChannelHandlerContext ctx, HttpRequest req);

    public abstract void onMessage(ChannelHandlerContext con, String message);

    public abstract void onClose(ChannelHandlerContext con);

    public abstract void httpReq(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res);

    public boolean send(ChannelHandlerContext con, String text) {
        try {

            con.channel().writeAndFlush(new TextWebSocketFrame(text));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }

    public void closeClient(ChannelHandlerContext con, String text) {
        ChannelFuture f = con.channel().writeAndFlush(new TextWebSocketFrame(text));

        f.addListener(ChannelFutureListener.CLOSE);

    }
    public void closeServer(){
        try {
          f.channel().close();
        } catch (Exception ex) {
            
        }
    }
}
